home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / gdb / core.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  11.6 KB  |  466 lines

  1. /* Work with core dump and executable files, for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include "defs.h"
  24. #include "param.h"
  25. #include "frame.h"  /* required by inferior.h */
  26. #include "inferior.h"
  27. #include "symtab.h"
  28. #include "command.h"
  29. #include "bfd.h"
  30. #include "target.h"
  31. #include "gdbcore.h"
  32.  
  33. extern int xfer_memory ();
  34. extern void child_attach (), child_create_inferior ();
  35.  
  36. extern int sys_nerr;
  37. extern char *sys_errlist[];
  38. extern char *sys_siglist[];
  39.  
  40. extern char registers[];
  41.  
  42. /* Hook for `exec_file_command' command to call.  */
  43.  
  44. void (*exec_file_display_hook) () = NULL;
  45.  
  46. struct section_table *core_sections, *core_sections_end;
  47.  
  48. /* Binary file diddling handle for the core file.  */
  49.  
  50. bfd *core_bfd = NULL;
  51.  
  52. /* Forward decl */
  53. extern struct target_ops core_ops;
  54.  
  55.  
  56. /* Discard all vestiges of any previous core file
  57.    and mark data and stack spaces as empty.  */
  58.  
  59. /* ARGSUSED */
  60. void
  61. core_close (quitting)
  62.      int quitting;
  63. {
  64.   if (core_bfd) {
  65.     free (bfd_get_filename (core_bfd));
  66.     bfd_close (core_bfd);
  67.     core_bfd = NULL;
  68. #ifdef CLEAR_SOLIB
  69.     CLEAR_SOLIB ();
  70. #endif
  71.   }
  72. }
  73.  
  74. /* This routine opens and sets up the core file bfd */
  75.  
  76. void
  77. core_open (filename, from_tty)
  78.      char *filename;
  79.      int from_tty;
  80. {
  81.   const char *p;
  82.   int siggy;
  83.   struct cleanup *old_chain;
  84.   char *temp;
  85.   bfd *temp_bfd;
  86.   int ontop;
  87.  
  88.   target_preopen (from_tty);
  89.   if (!filename)
  90.     {
  91.       error (core_bfd? 
  92.        "No core file specified.  (Use `detach' to stop debugging a core file.)"
  93.      : "No core file specified.");
  94.     }
  95.  
  96.   filename = tilde_expand (filename);
  97.   if (filename[0] != '/') {
  98.     temp = concat (current_directory, "/", filename);
  99.     free (filename);
  100.     filename = temp;
  101.   }
  102.  
  103.   old_chain = make_cleanup (free, filename);
  104.   temp_bfd = bfd_openr (filename, NULL);
  105.   if (temp_bfd == NULL)
  106.     {
  107.       perror_with_name (filename);
  108.     }
  109.  
  110.   if (!bfd_check_format (temp_bfd, bfd_core))
  111.     {
  112.       bfd_close (temp_bfd);
  113.       error ("\"%s\" does not appear to be a core dump", filename);
  114.     }
  115.  
  116.   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
  117.  
  118.   discard_cleanups (old_chain);        /* Don't free filename any more */
  119.   unpush_target (&core_ops);
  120.   core_bfd = temp_bfd;
  121.   old_chain = make_cleanup (core_close, core_bfd);
  122.  
  123.   validate_files ();
  124.  
  125.   /* Find the data section */
  126.   if (build_section_table (core_bfd, &core_sections, &core_sections_end))
  127.     error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
  128.        bfd_errmsg (bfd_error));
  129.  
  130.   ontop = !push_target (&core_ops);
  131.   make_cleanup (unpush_target, &core_ops);
  132.  
  133.   p = bfd_core_file_failing_command (core_bfd);
  134.   if (p)
  135.     printf ("Core file invoked as `%s'.\n", p);
  136.  
  137.   siggy = bfd_core_file_failing_signal (core_bfd);
  138.   if (siggy > 0)
  139.     printf ("Program terminated with signal %d, %s.\n", siggy,
  140.         siggy < NSIG ? sys_siglist[siggy] : "(undocumented)");
  141.  
  142.   if (ontop) {
  143.     /* Fetch all registers from core file */
  144.     target_fetch_registers (-1);
  145.     /* Add symbols for any shared libraries that were in use */
  146. #ifdef SOLIB_ADD
  147.     SOLIB_ADD (NULL, from_tty);
  148. #endif
  149.     /* Now, set up the frame cache, and print the top of stack */
  150.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  151.                       read_pc ()));
  152.     select_frame (get_current_frame (), 0);
  153.     print_sel_frame (0);    /* Print the top frame and source line */
  154.   } else {
  155.     printf (
  156. "Warning: you won't be able to access this core file until you terminate\n\
  157. your %s; do ``info files''\n", current_target->to_longname);
  158.   }
  159.  
  160.   discard_cleanups (old_chain);
  161. }
  162.  
  163. void
  164. core_detach (args, from_tty)
  165.      char *args;
  166.      int from_tty;
  167. {
  168.   if (args)
  169.     error ("Too many arguments");
  170.   unpush_target (&core_ops);
  171.   if (from_tty)
  172.     printf ("No core file now.\n");
  173. }
  174.  
  175. /* Backward compatability with old way of specifying core files.  */
  176.  
  177. void
  178. core_file_command (filename, from_tty)
  179.      char *filename;
  180.      int from_tty;
  181. {
  182.   dont_repeat ();            /* Either way, seems bogus. */
  183.   if (!filename)
  184.     core_detach (filename, from_tty);
  185.   else
  186.     core_open (filename, from_tty);
  187. }
  188.  
  189.  
  190. /* Call this to specify the hook for exec_file_command to call back.
  191.    This is called from the x-window display code.  */
  192.  
  193. void
  194. specify_exec_file_hook (hook)
  195.      void (*hook) ();
  196. {
  197.   exec_file_display_hook = hook;
  198. }
  199.  
  200. /* The exec file must be closed before running an inferior.
  201.    If it is needed again after the inferior dies, it must
  202.    be reopened.  */
  203.  
  204. void
  205. close_exec_file ()
  206. {
  207. #ifdef FIXME
  208.   if (exec_bfd)
  209.     bfd_tempclose (exec_bfd);
  210. #endif
  211. }
  212.  
  213. void
  214. reopen_exec_file ()
  215. {
  216. #ifdef FIXME
  217.   if (exec_bfd)
  218.     bfd_reopen (exec_bfd);
  219. #endif
  220. }
  221.  
  222. /* If we have both a core file and an exec file,
  223.    print a warning if they don't go together.
  224.    This should really check that the core file came
  225.    from that exec file, but I don't know how to do it.  */
  226.  
  227. void
  228. validate_files ()
  229. {
  230.   if (exec_bfd && core_bfd)
  231.     {
  232.       if (core_file_matches_executable_p (core_bfd, exec_bfd))
  233.     printf ("Warning: core file does not match specified executable file.\n");
  234.       else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
  235.     printf ("Warning: exec file is newer than core file.\n");
  236.     }
  237. }
  238.  
  239. /* Return the name of the executable file as a string.
  240.    ERR nonzero means get error if there is none specified;
  241.    otherwise return 0 in that case.  */
  242.  
  243. char *
  244. get_exec_file (err)
  245.      int err;
  246. {
  247.   if (exec_bfd) return bfd_get_filename(exec_bfd);
  248.   if (!err)     return NULL;
  249.  
  250.   error ("No executable file specified.\n\
  251. Use the \"file\" or \"exec-file\" command.");
  252.   return NULL;
  253. }
  254.  
  255. static void
  256. core_files_info ()
  257. {
  258.   struct section_table *p;
  259.  
  260.   printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd));
  261.  
  262.   for (p = core_sections; p < core_sections_end; p++)
  263.     printf("\tcore file  from 0x%08x to 0x%08x is %s\n",
  264.     p->addr, p->endaddr,
  265.     bfd_section_name (core_bfd, p->sec_ptr));
  266. }
  267.  
  268. void
  269. memory_error (status, memaddr)
  270.      int status;
  271.      CORE_ADDR memaddr;
  272. {
  273.  
  274.   if (status == EIO)
  275.     {
  276.       /* Actually, address between memaddr and memaddr + len
  277.      was out of bounds. */
  278.       error ("Cannot access memory at address 0x%x.", memaddr);
  279.     }
  280.   else
  281.     {
  282.       if (status >= sys_nerr || status < 0)
  283.     error ("Error accessing memory address 0x%x: unknown error (%d).",
  284.            memaddr, status);
  285.       else
  286.     error ("Error accessing memory address 0x%x: %s.",
  287.            memaddr, sys_errlist[status]);
  288.     }
  289. }
  290.  
  291. /* Same as target_read_memory, but report an error if can't read.  */
  292. void
  293. read_memory (memaddr, myaddr, len)
  294.      CORE_ADDR memaddr;
  295.      char *myaddr;
  296.      int len;
  297. {
  298.   int status;
  299.   status = target_read_memory (memaddr, myaddr, len);
  300.   if (status != 0)
  301.     memory_error (status, memaddr);
  302. }
  303.  
  304. /* Same as target_write_memory, but report an error if can't write.  */
  305. void
  306. write_memory (memaddr, myaddr, len)
  307.      CORE_ADDR memaddr;
  308.      char *myaddr;
  309.      int len;
  310. {
  311.   int status;
  312.  
  313.   status = target_write_memory (memaddr, myaddr, len);
  314.   if (status != 0)
  315.     memory_error (status, memaddr);
  316. }
  317.  
  318. /* Read an integer from debugged memory, given address and number of bytes.  */
  319.  
  320. long
  321. read_memory_integer (memaddr, len)
  322.      CORE_ADDR memaddr;
  323.      int len;
  324. {
  325.   char cbuf;
  326.   short sbuf;
  327.   int ibuf;
  328.   long lbuf;
  329.  
  330.   if (len == sizeof (char))
  331.     {
  332.       read_memory (memaddr, &cbuf, len);
  333.       return cbuf;
  334.     }
  335.   if (len == sizeof (short))
  336.     {
  337.       read_memory (memaddr, (char *)&sbuf, len);
  338.       SWAP_TARGET_AND_HOST (&sbuf, sizeof (short));
  339.       return sbuf;
  340.     }
  341.   if (len == sizeof (int))
  342.     {
  343.       read_memory (memaddr, (char *)&ibuf, len);
  344.       SWAP_TARGET_AND_HOST (&ibuf, sizeof (int));
  345.       return ibuf;
  346.     }
  347.   if (len == sizeof (lbuf))
  348.     {
  349.       read_memory (memaddr, (char *)&lbuf, len);
  350.       SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf));
  351.       return lbuf;
  352.     }
  353.   error ("Cannot handle integers of %d bytes.", len);
  354.   return -1;    /* for lint */
  355. }
  356.  
  357. /* Read or write the core file.
  358.  
  359.    Args are address within core file, address within gdb address-space,
  360.    length, and a flag indicating whether to read or write.
  361.  
  362.    Result is a length:
  363.  
  364.     0:    We cannot handle this address and length.
  365.     > 0:  We have handled N bytes starting at this address.
  366.           (If N == length, we did it all.)  We might be able
  367.           to handle more bytes beyond this length, but no
  368.           promises.
  369.     < 0:  We cannot handle this address, but if somebody
  370.           else handles (-N) bytes, we can start from there.
  371.  
  372.    The actual work is done by xfer_memory in exec.c, which we share
  373.    in common with exec_xfer_memory().  */
  374.  
  375. static int
  376. core_xfer_memory (memaddr, myaddr, len, write)
  377.      CORE_ADDR memaddr;
  378.      char *myaddr;
  379.      int len;
  380.      int write;
  381. {
  382.   int res;
  383.   res = xfer_memory (memaddr, myaddr, len, write,
  384.               core_bfd, core_sections, core_sections_end);
  385. #ifdef SOLIB_XFER_MEMORY
  386.   if (res == 0)
  387.     res = SOLIB_XFER_MEMORY (memaddr, myaddr, len, write);
  388. #endif
  389.   return res;
  390. }
  391.  
  392. /* Get the registers out of a core file.  This is the machine-
  393.    independent part.  Fetch_core_registers is the machine-dependent
  394.    part, typically implemented in the xm-file for each architecture.  */
  395.  
  396. /* We just get all the registers, so we don't use regno.  */
  397. /* ARGSUSED */
  398. static void
  399. get_core_registers (regno)
  400.      int regno;
  401. {
  402.   sec_ptr reg_sec;
  403.   unsigned size;
  404.   char *the_regs;
  405.  
  406.   reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
  407.   size = bfd_section_size (core_bfd, reg_sec);
  408.   the_regs = alloca (size);
  409.   if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
  410.                 (unsigned)0, size))
  411.     {
  412.       fetch_core_registers (the_regs, size, 0);
  413.     }
  414.   else
  415.     {
  416.       fprintf (stderr, "Couldn't fetch registers from core file: %s\n",
  417.            bfd_errmsg (bfd_error));
  418.     }
  419.  
  420.   /* Now do it again for the float registers, if they exist.  */
  421.   reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
  422.   if (reg_sec) {
  423.     size = bfd_section_size (core_bfd, reg_sec);
  424.     the_regs = alloca (size);
  425.     if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
  426.                   (unsigned)0, size))
  427.       {
  428.     fetch_core_registers (the_regs, size, 2);
  429.       }
  430.     else
  431.       {
  432.     fprintf (stderr, "Couldn't fetch register set 2 from core file: %s\n",
  433.          bfd_errmsg (bfd_error));
  434.       }
  435.   }
  436.   registers_fetched();
  437. }
  438.  
  439. struct target_ops core_ops = {
  440.     "core", "Local core dump file",
  441.     "Use a core file as a target.  Specify the filename of the core file.",
  442.     core_open, core_close,
  443.     child_attach, core_detach, 0, 0, /* resume, wait */
  444.     get_core_registers, 
  445.     0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
  446.     core_xfer_memory, core_files_info,
  447.     0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
  448.     0, 0, 0, 0, 0, /* terminal stuff */
  449.     0, 0, 0, 0, 0, /* kill, load, add_syms, call fn, lookup sym */
  450.     child_create_inferior, 0, /* mourn_inferior */
  451.     core_stratum, 0, /* next */
  452.     0, 1, 1, 1, 0,    /* all mem, mem, stack, regs, exec */
  453.     OPS_MAGIC,        /* Always the last thing */
  454. };
  455.  
  456. void
  457. _initialize_core()
  458. {
  459.  
  460.   add_com ("core-file", class_files, core_file_command,
  461.        "Use FILE as core dump for examining memory and registers.\n\
  462. No arg means have no core file.  This command has been superseded by the\n\
  463. `target core' and `detach' commands.");
  464.   add_target (&core_ops);
  465. }
  466.